package com.personnalManagerSystem.controller;


import com.personnalManagerSystem.model.Permission;
import com.personnalManagerSystem.model.Role;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liqing
 * @since 2022-03-27
 */
@Controller
@RequestMapping("/role")
public class RoleController extends  BaseController{

    @RequestMapping("/list")
    public String list(Model model,
                       String option,
                       String searchContent,
                       Integer pageNum,
                       Integer pageSize){
        baseList(model,option,searchContent,pageNum,pageSize,iRoleService);
        //一个角色对应多个权限查询权限列表
        List<Permission> permissionList = iPermissionService.list();
        model.addAttribute("permissionList",permissionList);
        return "view/role";
    }
    @RequestMapping("/add")
    public  String add(Role role,Model model){
        System.out.println("role:"+role);
        iRoleService.save(role);
        //查询所有角色信息,并把数据存储在model中
        List<Role> roleList = iRoleService.list();
        model.addAttribute("roleList",roleList);
        return "redirect:/role/list";
    }

    @RequestMapping("/edit")
    public String edit(Role role,Model model){
        iRoleService.saveOrUpdate(role);
        //查询所有角色信息,并把数据存储在model中
        List<Role> roleList = iRoleService.list();
        model.addAttribute("roleList",roleList);
        return "redirect:/role/list";
    }
    @RequestMapping("/del")
    public String del(@RequestParam("roleId") Integer roleId){
        System.out.println("roleId="+roleId);
        iRoleService.removeById(roleId);
        return "redirect:/role/list";
    }

}

